package searchteam.beans.stateless;

import java.util.ArrayList;
import java.util.Collection;

import javax.ejb.*;
import javax.persistence.*;

import searchteam.beans.entities.*;
import searchteam.client.beansMirrors.*;

@Stateless(mappedName="searchTeamFacade")
@Remote(SearchTeamFacadeRemote.class)
public class SearchTeamFacade implements SearchTeamFacadeRemote {

    @PersistenceContext
	private EntityManager entityManager;

    @SuppressWarnings("unchecked")
	public Collection<PersonMirror> getAllPersons() {
    	Collection<Person> persons = entityManager.createQuery("select person from Person person order by person.name, person.firstname").getResultList();
		
		Collection<PersonMirror> personsMirrors = new ArrayList<PersonMirror>();
		for(Person person : persons) {
			personsMirrors.add(person.getMirror());
		}
		
		return personsMirrors;	
	}
	
	public int createPerson(PersonMirror person) {
		Person createdPerson = new Person();
		createdPerson.setFirstname(person.getFirstname());
		createdPerson.setName(person.getName());
		createdPerson.setMail(person.getMail());
		createdPerson.setPhone(person.getPhone());
	
		entityManager.persist(createdPerson);
		
		return createdPerson.getId();
	}
	
	public int updatePerson(PersonMirror person) {
		Person updatedPerson = entityManager.find(Person.class, person.getId());
		updatedPerson.setFirstname(person.getFirstname());
		updatedPerson.setName(person.getName());
		updatedPerson.setMail(person.getMail());
		updatedPerson.setPhone(person.getPhone());
		
		if(person.getTeam() != null) {
			updatedPerson.setTeam(entityManager.find(Team.class, person.getTeam().getId()));
		}
		else {
			updatedPerson.setTeam(null);
		}

		return updatedPerson.getId();
	}
	
	@SuppressWarnings("unchecked")
	public Collection<PaperMirror> getAllPapers() {
		Collection<Paper> papers = entityManager.createQuery("select paper from Paper paper order by paper.title").getResultList();
		
		Collection<PaperMirror> papersMirrors = new ArrayList<PaperMirror>();
		for(Paper paper : papers) {
			papersMirrors.add(paper.getMirror());
		}
		
		return papersMirrors;
	}
	
	public int createPaper(PaperMirror paper) {
		Paper createdPaper = new Paper();
		createdPaper.setTitle(paper.getTitle());
		createdPaper.setState(paper.getState());
		
		entityManager.persist(createdPaper);
		
		return createdPaper.getId();
	}
	
	public int updatePaper(PaperMirror paper) {
		Paper updatedPaper = new Paper();
		updatedPaper.setTitle(paper.getTitle());
		updatedPaper.setState(paper.getState());

		return updatedPaper.getId();
	}

	@SuppressWarnings("unchecked")
	public Collection<TeamMirror> getAllTeams() {
		Collection<Team> teams = entityManager.createQuery("select team from Team team order by team.name").getResultList();
		
		Collection<TeamMirror> teamsMirror = new ArrayList<TeamMirror>();
		for(Team team : teams) {
			teamsMirror.add(team.getMirror());
		}
		
		return teamsMirror;
	}

	public int createTeam(TeamMirror team) {
		Team createdTeam = new Team();
		createdTeam.setName(team.getName());
		createdTeam.setMeaning(team.getMeaning());
		createdTeam.setStatus(team.getStatus());
		
		createdTeam.setTeamMaster(entityManager.find(Person.class, team.getTeamMaster().getId()));
		
		entityManager.persist(createdTeam);
		
		return createdTeam.getId();
	}
	
	public int updateTeam(TeamMirror team) {
		Team updatedTeam = entityManager.find(Team.class, team.getId());
		updatedTeam.setName(team.getName());
		updatedTeam.setMeaning(team.getMeaning());
		updatedTeam.setStatus(team.getStatus());
		
		
		updatedTeam.setTeamMaster(entityManager.find(Person.class, team.getTeamMaster().getId()));
		
		Collection<PersonMirror> personsMirrors = team.getMembers();
	
		for(PersonMirror personMirror : personsMirrors) {
			Person person = entityManager.find(Person.class, personMirror.getId());
			updatedTeam.addMember(person);
		}
		
		return updatedTeam.getId();
	}
	
	public void addPersonToTeam(PersonMirror person, TeamMirror team) {
		Person foundPerson = entityManager.find(Person.class, person.getId());
		Team foundTeam = entityManager.find(Team.class, team.getId());
		
		foundTeam.addMember(foundPerson);
	}
	
	public void linkPersonsToPaper(Collection<PersonMirror> persons, PaperMirror paper) {
		Paper foundPaper = entityManager.find(Paper.class, paper.getId());
		
		Collection<Person> authorsToDelete = new ArrayList<Person>();
		for(Person person : foundPaper.getAuthors()) {
			authorsToDelete.add(person);
		}
		
		for(Person person : authorsToDelete) {
			foundPaper.removeAuthor(person);
		}
		
		for(PersonMirror person : persons) {
			Person foundPerson = entityManager.find(Person.class, person.getId());
			foundPaper.addAuthor(foundPerson);
		}
	}
	
	public void linkPapersToPerson(Collection<PaperMirror> papers, PersonMirror person) {
		Person foundPerson = entityManager.find(Person.class, person.getId());
		
		for(Paper paper : foundPerson.getPapers()) {
			paper.removeAuthor(foundPerson);
		}
		
		for(PaperMirror paper : papers) {
			Paper foundPaper = entityManager.find(Paper.class, paper.getId());
			foundPaper.addAuthor(foundPerson);
		}
	}
	
	@SuppressWarnings("unchecked")
	public Collection<PaperMirror> getPapersOfTeam(TeamMirror team) {
		Collection<Paper> papers = entityManager.createQuery("select paper from Team team, Person person, Paper paper where person.team = ?1 and person member of paper.authors").setParameter(1, team).getResultList();
		
		Collection<PaperMirror> papersMirrors = new ArrayList<PaperMirror>();
		for(Paper paper : papers) {
			papersMirrors.add(paper.getMirror());
		}
		
		return papersMirrors;
	}

}
